<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="form" ref="formRef" label-width="120px">
        <el-form-item label="API密钥">
          <el-input v-model="form.apiKey" type="password" placeholder="请输入API密钥" />
        </el-form-item>
        <el-form-item label="默认模型">
          <el-select v-model="form.defaultModel" placeholder="请选择默认模型">
            <el-option label="GPT-4" value="gpt-4" />
            <el-option label="GPT-3.5" value="gpt-3.5-turbo" />
          </el-select>
        </el-form-item>
        <el-form-item label="最大令牌数">
          <el-input-number v-model="form.maxTokens" :min="1" :max="4096" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Check" class="toolbar-button is-primary" @click="handleSave">保存配置</el-button>
          <el-button icon="Refresh" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 历史配置 -->
    <el-card class="toolbar-card">
      <template #header>
        <div class="card-header">
          <span>历史配置</span>
          <el-button
            type="primary"
            icon="Plus"
            class="toolbar-button is-primary"
            @click="handleAdd"
          >新增配置</el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="configHistory" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="model" label="AI模型" />
        <el-table-column prop="maxTokens" label="最大令牌数" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template #default="scope">
            <div class="table-operation-wrapper">
              <el-button
                type="primary"
                icon="Edit"
                @click="handleEdit(scope.row)"
                class="table-operation-button is-primary"
                size="small"
              >修改</el-button>
              <el-button
                type="danger"
                icon="Delete"
                @click="handleDelete(scope.row)"
                class="table-operation-button is-danger"
                size="small"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const formRef = ref(null)
const loading = ref(false)

const form = reactive({
  apiKey: '',
  defaultModel: 'gpt-4',
  maxTokens: 2048
})

const configHistory = ref([
  {
    id: 1,
    model: 'GPT-4',
    maxTokens: 2048,
    createTime: '2024-06-18 10:30:00'
  },
  {
    id: 2,
    model: 'GPT-3.5-turbo',
    maxTokens: 4096,
    createTime: '2024-06-17 15:45:00'
  }
])

const handleSave = () => {
  ElMessage.success('配置保存成功')
}

const resetForm = () => {
  formRef.value?.resetFields()
}

const handleAdd = () => {
  ElMessage.info('新增配置功能开发中...')
}

const handleEdit = (row) => {
  form.defaultModel = row.model
  form.maxTokens = row.maxTokens
  ElMessage.info('加载配置成功')
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该配置吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
  }).catch(() => {})
}

onMounted(() => {
  // 初始化数据
})
</script>

<style lang="scss" scoped>
.search-card {
  margin-bottom: 16px;
}

.toolbar-card {
  margin-bottom: 16px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mb8 {
  margin-bottom: 8px;
}

.table-operation-wrapper {
  display: flex !important;
  flex-wrap: wrap !important;
  gap: 5px;
  justify-content: center;
  align-items: center !important;
  margin: -4px !important;
}
</style>
